﻿using B.S.Business.Domain;
using B.S.Business.ErrorCode;
using B.S.Business.Read.API.Applications.Command.SupplierCommand;
using B.S.Business.Read.API.Applications.Dtos;
using B.S.Business.Read.API.Applications.Export;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Org.BouncyCastle.Asn1.Cms;

namespace B.S.Business.Read.API.Controllers
{
    /// <summary>
    /// 供应商 读-Api控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManagSupplierController : ControllerBase
    {
        private readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public ManagSupplierController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 供应商分页查询显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<SupplierApp>> SupplierQuery([FromQuery] SupplierQueryCommand command)
        {
            return mediator.Send(command);
        }

        /// <summary>
        /// 导出供应商查询
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// <summary>
        [HttpGet]
        public Task<ApiResult<List<SupplierCommodityDto>>> SupplierExportQuser([FromQuery] SupplierExportQuserCommand command)
        {
            return mediator.Send(command);
        }

        /// <summary>
        /// 供应商导出
        /// </summary>
        /// <param name="SupplierId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> SupplierExportOrderList([FromQuery] List<int> SupplierId)
        {
            if (SupplierId == null || !SupplierId.Any())
            {
                return BadRequest("请提供应商 ID 列表。");
            }

            var request = new SupplierExportQuserCommand { SupplierId = SupplierId };
            var result = await SupplierExportQuser(request);

            if (result.Data?.Any() != true)
                return NotFound("未找到供应商数据。");

            using IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("供应商列表");
            IRow headerRow = sheet.CreateRow(0);

            // 设置表头
            string[] headers = { "供应商主键", "供应商编号", "供应商名称", "类型", "电话", "传真", "邮箱", "逻辑删除" };
            for (int i = 0; i < headers.Length; i++)
                headerRow.CreateCell(i).SetCellValue(headers[i]);

            // 填充数据行
            int rowIndex = 1;
            foreach (var item in result.Data) // 直接使用 result.Data 集合
            {
                IRow row = sheet.CreateRow(rowIndex++);
                row.CreateCell(0).SetCellValue(item.SupplierId);
                row.CreateCell(1).SetCellValue(item.SupplierNumber);
                row.CreateCell(2).SetCellValue(item.SupplierName);
                row.CreateCell(3).SetCellValue(item.OrderAmount);
                row.CreateCell(4).SetCellValue(item.SupplierPhone);
                row.CreateCell(5).SetCellValue(item.ChuanZhen);
                row.CreateCell(6).SetCellValue(item.Email);
                row.CreateCell(8).SetCellValue((bool)item.IsDeleted ? "是" : "否");
            }
            using var ms = new MemoryStream();
            book.Write(ms);
            byte[] buffer = ms.ToArray();

            return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "供应商列表.xlsx");
        }
    }
}
